package com.bb.blog.security.service.wx;

import com.bb.blog.web.exception.ServerException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

@RequiredArgsConstructor
public class WxSessionService {
    private final WxRequestFactory wxRequestFactory;
    private final RestTemplate restTemplate;

    WxUser getWxUser(String code){
        RequestEntity requestEntity = wxRequestFactory.codeToSession(code);
        ResponseEntity<WxUser> responseEntity = restTemplate.exchange(requestEntity, WxUser.class);
        if(responseEntity.getBody()!= null && responseEntity.getBody().getErrcode()!=null){
            throw new ServerException(500, "访问微信服务端异常");
        }
        return responseEntity.getBody();


    }
}
